import Dialog from '@vant/weapp/dialog/dialog';
let app = getApp()
import { request } from "../../utils/request.js";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        fileList: [
            {
                url: '',
                name: '',
            }
        ],
        name: '',
        phone: '',
        oldPassword: '',
        newPassword: '',
        showDialog: false,
        configColor: app.globalData.configColor,
        configColorForOnlyStyle: app.globalData.configColorForOnlyStyle,
        statusBarHeight: app.globalData.statusBarHeight,
        navBarHeight: app.globalData.navBarHeight,
    },
    go_index() {
        wx.switchTab({
            url: '/pages/my/my',
        });
    },
    afterRead(event) {
        let that = this;
        const { file } = event.detail;
        // console.log(file.url) 图片临时地址
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        wx.uploadFile({
            url: app.globalData.url + '/uploadAvatar',
            filePath: file.url,
            name: 'file',
            //   formData: { user: 'test' },
            success(res) {
                // 上传完成需要更新 fileList
                // const { fileList = [] } = this.data;
                // fileList.push({ ...file, url: res.data });
                // this.setData({ fileList });
                let resp = JSON.parse(res.data)
                if (resp.flag) {
                    that.setData({
                        'fileList[0].url': resp.data
                    });
                } else {
                    wx.showToast({
                        title: '图片上传失败:' + resp.message,
                        icon: 'none'
                    })
                }
            },
        });


        // request({   //调用请求方法
        //     url: "/uploadAvatar",   
        //     method: "POST",
        //     filePath: file.url,
        //     headerObj: {
        //         "Content-Type": "multipart/form-data",        
        //     },
        //     name: 'file',
        //   }) .then((responseData) => {


        // })


    },

    update_personl() {
        this.setData({
            showDialog: true
        })
        Dialog.confirm({
            title: '确认修改吗?'
        }).then(() => {
            wx.request({
                url: app.globalData.url + '/updateUserInfo',
                header: {
                    "Content-Type": "application/json"
                },
                method: 'POST',
                data: {
                    id: wx.getStorageSync('userInfo').id,
                    name: this.data.name,
                    phone: this.data.phone,
                    oldPassword: this.data.oldPassword,
                    newPassword: this.data.newPassword,
                    avatar: this.data.fileList[0].url
                },
                success: (res) => {
                    let responseData = res.data
                    // console.log(responseData)
                    if (responseData.flag) {
                        wx.showToast({
                            title: '修改成功',
                            icon: 'success'
                        })
                        wx.setStorageSync('userInfo', responseData.data)
                        wx.switchTab({
                            url: '/pages/my/my',
                        })
                    } else {
                        wx.showToast({
                            title: '修改失败:' + responseData.message,
                            icon: 'none'
                        })
                    }
                },
                fail: (err) => {
                    wx.showToast({
                        title: '删除失败，请稍后尝试',
                        icon: 'none'
                    })
                }
            })

        })
        this.setData({
            showDialog: false
        })
    },



    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            'fileList[0].url': wx.getStorageSync("userInfo").avatar,
            name: wx.getStorageSync("userInfo").name,
            phone: wx.getStorageSync("userInfo").phone,
        });


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if (wx.getStorageSync('configColor') !== this.data.configColor
            || wx.getStorageSync('configColorForOnlyStyle') !== this.data.configColorForOnlyStyle) {
            this.setData({ configColor: wx.getStorageSync('configColor') });
            this.setData({ configColorForOnlyStyle: wx.getStorageSync('configColorForOnlyStyle') });
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})